'use strict';
var offset = 45;
(function (exports) {
    var OpenCards = cc.Layer.extendEX({
        ctor: function () {
            this._super();
            // this.setScale(0.8);
            this.removeAllChildren();
            return this;
        },
        setPos: function (pos) {
            this.pos = pos;
            mAction.combos[this.pos] = [];
            this.removeAllChildren();
        },
        /**
         * 用于小结桌面显示他人的
         * @param cards
         * @param typ
         * @param chiPaiId
         */
        addCardsByOtherPlayer: function(cards, typ, chiPaiId){
            var self = this;

        },
        /**
         * 亮牌
         * @param cards
         * @param typ
         * @param isOpen
         * @param chiPaiId
         * @param typeHuxi
         * @param isSimulation1 模拟当前玩家的显示
         */
        addCards: function (cards, typ, isOpen,chiPaiId,typeHuxi, isSimulation1) {
            var that = this;
            var comboList = mAction.combos[this.pos];
            var comboInfo = {};
            comboInfo.typ = typ;
            comboInfo.cards = cards;
            comboInfo.isOpen = isOpen;
            comboInfo.chiPaiId = chiPaiId;
            var colIndex = 0;
            var smallSize = mCard.getSmallSize();

            if (isOpen == true) {
                var card = comboInfo.cards[0];
                for (var i = 0; i < comboList.length; i++) {
                    var comb = comboList[i];
                    var cardInCombo = comb.cards[0];
                    if (card == cardInCombo) {
                        if (comb.typ == mCard.comboTypes.peng ||
                            comb.typ == mCard.comboTypes.wei ||
                            comb.typ == mCard.comboTypes.chouwei) {
                            comb.cards = cards;
                            comb.typ = typ;
                            colIndex = i + 1;
                            break;
                        }
                    }
                }
                var children = this.getChildren();
                for (var i = children.length - 1; i >= 0; i--) {
                    var child = children[i];
                    if (child && child.getTag() == colIndex) {
                        child.removeFromParent();
                    }
                }
                var children = this.getChildren();
                for (var i = 0; i < children.length; i++) {
                    //移动之前的位置 如果跑的是之前的牌,移动到最后面的位置,之前的往前移动
                    var child = children[i];
                    if (child.getTag() > colIndex) {
                        var tag = child.getTag();
                        tag = tag - 1;
                        if (ZiPaiRoomData.playerNum < 4) {
                            if(gameData.mapId == app.map_id.XIANGXI_ERQISHI){
                                if (this.pos == 1) {
                                    cc.log('tag'+tag);
                                    child.setPositionX(270 - (tag - 1) * smallSize.w);
                                } else {
                                    child.setPositionX((tag - 1) * smallSize.w + 24);
                                }
                            }else{
                                if (this.pos == 1) {
                                    child.setPositionX(270 - (tag - 1) * smallSize.w);
                                } else {
                                    child.setPositionX((tag - 1) * smallSize.w + 24);
                                }
                            }
                        } else {
                            //todo 四人玩以后处理
                            if (mRoom.wanfatype == mRoom.HENGYANG || mRoom.wanfatype == mRoom.HENGDONG || mRoom.wanfatype == mRoom.MAOHUZI) {
                                if (this.pos == 1 || this.pos == 2) {
                                    child.setPositionX(150 - (tag - 1) * smallSize.w);
                                } else {
                                    child.setPositionX((tag - 1) * smallSize.w + 24);
                                }
                            } else {
                                if (this.pos == 1) {
                                    child.setPositionX(150 - (tag - 1) * smallSize.w);
                                } else {
                                    child.setPositionX((tag - 1) * smallSize.w + 24);
                                }
                            }
                        }
                        child.setTag(tag);
                    }
                }
                //交换数据
                var tmp = null;
                if (colIndex >= 1) {
                    tmp = comboList[colIndex - 1];
                } else {
                    tmp = comboList[0];
                }
                for (var i = 0; i < comboList.length - 1; i++) {
                    if (i >= colIndex - 1) {
                        comboList[i] = comboList[i + 1];
                    }
                }
                comboList[comboList.length - 1] = tmp;
                colIndex = comboList.length;
            } else {
                comboList.push(comboInfo);
                colIndex = comboList.length;
            }
            var isHide = false;
            var isHideAll = false;
            if (typ == mCard.comboTypes.wei) {
                isHideAll = true;
                isHide = true;
            }
            else if (typ == mCard.comboTypes.ti) {
                isHide = true;
            }
            else if (typ == mCard.comboTypes.chouwei) {
                isHide = true;
            }

            var isSelfHide = false;
            if (isHide == true && (this.pos == 0 || isSimulation1 == true)) {
                isSelfHide = true;
            }
            if (cards.length == 3 && (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) && chiPaiId){
                var chipaiI = 0;
                for (var i = 0; i < 3; i++){
                    if (cards[i] == chiPaiId){
                        chipaiI = i;
                    }
                }
                var tmpCards = cards;
                cards = [];
                for (var i = 0; i < 3; i++){
                    if (i != chipaiI) {
                        cards.push(tmpCards[i]);
                    }
                }
                cards[2] = tmpCards[chipaiI];
            }

            var showChiIndex = false;
            for (var j = 0; j < cards.length; j++) {
                var newpos = this.getPosByPlayerNum(colIndex, j, cards.length);
                var card = new Card();
                this.addChild(card);
                card.setName('card' + j);
                var cardMask = null;
                if (isHide == true && (j < cards.length - 1 || isHideAll == true)) {
                    if (isSelfHide == true) {
                        if ((gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) && (typ == mCard.comboTypes.wei || typ == mCard.comboTypes.ti)) {
                            card.setData(cards[j], null, 4);
                        } else {
                            card.setData(cards[j], null, 2);
                        }
                        cardMask = new Card();
                        this.addChild(cardMask);
                        cardMask.setData(cards[j], null, 2, isHide);
                        cardMask.setOpacity(150);
                        cardMask.setPosition(newpos);
                        cardMask.setTag(colIndex);
                        cardMask.setName('cardMask' + j);

                    } else {
                        card.setData(cards[j], null, 2, isHide);
                    }
                }
                else {
                    if ((gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) && typ == mCard.comboTypes.ti) {
                        card.setData(cards[j], null, 4);
                        cardMask = new Card();
                        this.addChild(cardMask);
                        cardMask.setData(cards[j], null, 2, isHide);
                        cardMask.setOpacity(255);
                        cardMask.setPosition(newpos);
                        cardMask.setTag(colIndex);
                        cardMask.setName('cardMask' + j);
                    }
                    else {
                        card.setData(cards[j], null, 2);
                    }
                }
                card.setPosition(newpos);
                card.setTag(colIndex);
                var canFanzhuan = false;
                if (ZiPaiRoomData.isReplay == true) {
                    //臭偎  后两张盖着
                    if (typ == mCard.comboTypes.chouwei) {
                        if (j != 2) {
                            card.resetCardBackImg();
                            if (cardMask) cardMask.resetCardBackImg();
                        } else {
                            if (card) card.setData(cards[j], null, 2);
                            if (cardMask) cardMask.setData(cards[j], null, 2);
                        }
                    }
                    //提  后三张盖着
                    if (typ == mCard.comboTypes.ti) {
                        if (j != 3 && gameData.mapId != app.map_id.QYLDS && gameData.mapId != app.map_id.YZCHZ) {
                            card.resetCardBackImg();
                            if (cardMask) cardMask.resetCardBackImg();
                        } else {
                            if (card) card.setData(cards[j], null, 2);
                            if (cardMask) cardMask.setData(cards[j], null, 2);
                        }
                    }
                    //偎的牌  点击才反过来
                    if (typ == mCard.comboTypes.wei) {
                        canFanzhuan = false;
                        var pre = 'small_';
                        var cardnum = parseInt(cards[j]);
                        if (parseInt(cards[j]) > 10) {
                            pre = 'small_';
                            cardnum = cardnum - 10;
                        }
                        card.imgSrc = mCard.getCardImg(pre + cardnum);
                    }
                }
                else {
                    //臭偎  后两张盖着
                    if (typ == mCard.comboTypes.chouwei && (this.pos == 0 || isSimulation1 == true)) {
                        if (j != 2) {
                            if (card) card.setOpacity(0);
                            if (cardMask) cardMask.setOpacity(255);
                        }
                    }
                    //提  后三张盖着
                    if (typ == mCard.comboTypes.ti && (this.pos == 0 || isSimulation1 == true)) {
                        if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ){
                            if (cardMask) cardMask.setOpacity(0);
                        }
                        else {
                            if (j != 3) {
                                if (card) card.setOpacity(0);
                                if (cardMask) cardMask.setOpacity(255);
                            }
                        }
                    }
                    //衡东六胡抢   提 只有自己能看  其他人看都是盖着的
                    if (mRoom.wanfatype == mRoom.HENGDONG) {
                        if (typ == mCard.comboTypes.ti && this.pos != 0) {
                            card.resetCardBackImg();
                            if (cardMask) cardMask.resetCardBackImg();
                        }
                    }
                    //偎的牌  点击才反过来
                    if (typ == mCard.comboTypes.wei && (this.pos == 0 || isSimulation1)) {
                        //if (mRoom.wanfatype == mRoom.YOUXIAN) {
                        //攸县碰胡子
                        if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ){
                            if (cardMask) cardMask.setOpacity(0);
                        }
                        else {
                            if (j != 2) {
                                if (cardMask) cardMask.setOpacity(255);
                            } else {
                                if (card) card.setData(cards[j], null, 2);
                                if (cardMask) cardMask.setData(cards[j], null, 2);
                                if (cardMask) cardMask.setOpacity(0);
                            }
                            canFanzhuan = false;
                            // }
                            // else {
                            //     canFanzhuan = true;
                            // }
                        }
                    }
                    //吃的牌红色显示
                    if (typ == mCard.comboTypes.o123 || typ == mCard.comboTypes.o2710 || typ == mCard.comboTypes.o234 ||
                        typ == mCard.comboTypes.jiao || typ == mCard.comboTypes.chi) {
                        if(cc.isUndefined(chiPaiId)){
                            if (j == 2) {
                                card.setChiCard();
                            }
                        }else{
                            if (cards[j] == chiPaiId) {
                                if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ){
                                    if (j == 2) card.setChiCard();
                                }
                                else {
                                    card.setChiCard();
                                }
                            }
                        }
                    }
                }
                if (canFanzhuan) {
                    card.resetCardBackImg();
                    if (cardMask) {
                        cardMask.setOpacity(255);
                    }
                    that.canFlap = true;
                    TouchUtil.setOnclickListener(card, function (node) {
                        if (that.canFlap == false) return;
                        var curindex = node.getTag();
                        that.hideColIndex(curindex, false);
                        that.canFlap = false;
                        for (var s = 0; s < 3; s++) {
                            (function (curs) {
                                var flapcard = that.getChildByName('flapcard' + curindex + curs);
                                if (flapcard == null) {
                                    var sceneid = settingData.local_table[gameData.mapId] || 0;
                                    flapcard = new cc.Sprite(zipai.res['huxiBG' + sceneid]);
                                    cc.log('flapcard',node.getPositionX());
                                    flapcard.setPosition(cc.p(node.getPositionX(), curs * smallSize.w + smallSize.w));
                                    flapcard.setName('flapcard' + colIndex + curs);
                                    that.addChild(flapcard, 1);
                                }
                                if (flapcard) {
                                    flapcard.setVisible(true);
                                    var action = cc.sequence(
                                        cc.scaleTo(0.3, -1, 1)
                                        , cc.callFunc(function () {
                                            var frame = cc.spriteFrameCache.getSpriteFrame(node.imgSrc);
                                            flapcard.setSpriteFrame(frame);
                                            flapcard.setFlippedX(true);
                                            flapcard.setFlippedX(true);
                                        })
                                        , cc.delayTime(1)
                                        , cc.scaleTo(0.3, 1, 1)
                                        , cc.callFunc(function () {
                                            var frame = cc.spriteFrameCache.getSpriteFrame(node.imgSrc);
                                            flapcard.setSpriteFrame(frame);
                                            flapcard.setFlippedX(true);
                                            flapcard.setVisible(false);
                                            that.canFlap = true;
                                            that.hideColIndex(curindex, true);
                                        })
                                    );
                                    flapcard.runAction(action);
                                }
                            })(s);
                        }
                    });
                }
            }
            if(gameData.wanfatype == 'go'){
                var hx  = mCard.getHuXi(comboList);
                var showHx = mCard.getHuXi(comboList, (this.pos == 0) ? true : false);
                mAction.hx[this.pos] = hx;
                mAction.showHx[this.pos] = showHx;
            } else {
                if (isSimulation1 == false) {
                    typeHuxi = !typeHuxi ? 0 : typeHuxi;
                    mAction.hx[this.pos] += typeHuxi;
                    mAction.showHx[this.pos] += typeHuxi;
                }
            }
          
        },
        //把偎的牌设置成 第一张明  后两张暗
        setWeiColor: function (pos) {
            var comboList = mAction.combos[pos];
            for (var i = 0; i < comboList.length; i++) {
                var comb = comboList[i];
                if (comb.typ == mCard.comboTypes.wei) {
                    var children = this.getChildren();
                    for (var j = children.length - 1; j >= 0; j--) {
                        var child = children[j];
                        if (child && child.getTag() == (i + 1)) {
                            if (child.getName() == 'card0' || child.getName() == 'card1') {
                                child.setData(comb.cards[0], null, 2, true);
                                child.setOpacity(255);

                                var cardMask = new Card();
                                cardMask.setData(comb.cards[0], null, 2);
                                cardMask.setAnchorPoint(0, 0);
                                cardMask.setOpacity(160);
                                child.addChild(cardMask);
                            } else if (child.getName() == 'card2') {
                                child.setData(comb.cards[0], null, 2);
                                child.setOpacity(255);
                            }
                        }
                    }
                }
            }
        },
        getPosByPlayerNum: function (colIndex, j, cardlength) {
            var smallSize = mCard.getSmallSize();
            var newpos = null;
            //祁阳落地扫的布局
            if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ) {
                if (ZiPaiRoomData.playerNum == 4) {
                    if (this.pos == 0) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 0.5 * smallSize.w, j * smallSize.h + 0.5 * smallSize.h);
                    } else if (this.pos == 1) {
                        newpos = cc.p(-(colIndex - 1) * smallSize.w - 0.5 * smallSize.w, j * smallSize.h + 0.5 * smallSize.h);
                    } else if (this.pos == 2) {
                        newpos = cc.p(-(colIndex - 1) * smallSize.w - 0.5 * smallSize.w, -j * smallSize.h - 0.5 * smallSize.h);
                    } else {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 0.5 * smallSize.w, -j * smallSize.h - 0.5 * smallSize.h);
                    }
                }
                else if (ZiPaiRoomData.playerNum == 3) {
                    if (this.pos == 0) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 0.5 * smallSize.w, j * smallSize.h + 0.5 * smallSize.h);
                    } else if (this.pos == 1) {
                        newpos = cc.p(-(colIndex - 1) * smallSize.w - 0.5 * smallSize.w, -j * smallSize.h - 0.5 * smallSize.h);
                    } else if (this.pos == 2) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 0.5 * smallSize.w, -j * smallSize.h - 0.5 * smallSize.h);
                    }
                }
                else if (ZiPaiRoomData.playerNum == 2) {
                    if (this.pos == 0) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 0.5 * smallSize.w, j * smallSize.h + 0.5 * smallSize.h);
                    } else if (this.pos == 1) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 0.5 * smallSize.w, -j * smallSize.h - 0.5 * smallSize.h);
                    }
                }
                return newpos;
            }
            else if (ZiPaiRoomData.playerNum < 4) {
                if(gameData.mapId == app.map_id.XIANGXI_ERQISHI){
                    if (this.pos == 1) {
                        newpos = cc.p(- (colIndex - 1) * smallSize.w, j * smallSize.h + 25 - smallSize.h);
                    } else {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 24, j * smallSize.h + 25 - smallSize.h);
                    }
                    return newpos;
                }
                if (this.pos == 1) {
                    newpos = cc.p(270 - (colIndex - 1) * smallSize.w, j * smallSize.h + 25 - smallSize.h);
                } else {
                    newpos = cc.p((colIndex - 1) * smallSize.w + 24, j * smallSize.h + 25 - smallSize.h);
                }
                if (this.pos != 0) {
                    newpos.y += (cardlength == 3) ? smallSize.h : 0;
                }
                return newpos;
            } else {
                //todo 四人完以后处理
                //碰胡子  1 2 位置 是行下往上
                if (mRoom.wanfatype == mRoom.YOUXIAN) {
                    if (this.pos == 1) {
                        newpos = cc.p(150 - (colIndex - 1) * smallSize.w, -j * smallSize.h + 130);
                    } else if (this.pos == 2) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 24, -j * smallSize.h + 130);
                    } else if (this.pos == 0 || this.pos == 3) {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 24, j * smallSize.h + 25 - smallSize.h);
                    }
                } else if (mRoom.wanfatype == mRoom.HENGYANG || mRoom.wanfatype == mRoom.HENGDONG || mRoom.wanfatype == mRoom.MAOHUZI) {
                    newpos = cc.p((this.pos == 0 || this.pos == 3) ? (colIndex - 1) * smallSize.w + 24 : 150 - (colIndex - 1) * smallSize.w,
                        (this.pos == 0 || this.pos == 1) ? j * smallSize.h + 25 - smallSize.h : j * smallSize.h + 25 - smallSize.h);
                }
                else {
                    if (this.pos == 1) {
                        newpos = cc.p(150 - (colIndex - 1) * smallSize.w, j * smallSize.h + 25 - smallSize.h);
                    } else {
                        newpos = cc.p((colIndex - 1) * smallSize.w + 24, j * smallSize.h + 25 - smallSize.h);
                    }
                }
                return newpos;
            }
        },
        hideColIndex: function (colIndex, flag) {
            var children = this.getChildren();
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (child.getTag() == colIndex) {
                    child.setVisible(flag);
                }
            }
        },
        //偎  结束后翻过牌来
        openWei: function (colIndex, card) {
            var children = this.getChildren();
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (child.getTag() == colIndex && child.getPositionY() >= 50) {
                    child.setData(card, null, 2);
                }
            }
        },
        getColIndex: function () {
            var max = 0;
            var children = this.getChildren();
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (max < child.getTag()) {
                    max = child.getTag();
                }
            }
            return max;
        },
        getCardColIndex: function (card) {
            var index = 0;
            var children = this.getChildren();
            var cardindex = 0;
            for (var i = 0; i < children.length; i++) {
                var child = children[i];
                if (child.data == card[cardindex]) {
                    cardindex++;
                    if (cardindex == card.length) {
                        return child.getTag();
                    }
                }
            }
            return index;
        },
    });
    exports.OpenCards = OpenCards;
})(window);
